// index.js
const app = getApp()
import {
    request_post
} from '../../utils/request';
Page({
    data: {
        user: null,
        notice: [],
        tips: null,
        usedTime: '',
        daySecond: 24 * 60 * 60,
        hourSecond: 60 * 60,
        minuteSecond: 60
    },
    onShow() {        
        this.setData({
            user: app.globalData.student,
            usedTime: this._getUsedTimeStr(app.globalData.student ? parseInt(app.globalData.student.usedTimeSecond) : 0)
        })
        request_post('/student/home').then(res => {
            this.setData({
                tips: res.noticeTips,
                notice: res.notice,
            })
        })
    },
    _getUsedTimeStr(baseTime) {
        let usedTimeSecond = parseInt((new Date().getTime() - app.globalData.startTime) / 1000) + baseTime;
        if (usedTimeSecond < 60) {
            return '不足1分钟'
        }
        let usedTimeStr = '';
        if (usedTimeSecond > this.data.daySecond) { //day
            usedTimeStr += parseInt(usedTimeSecond / this.data.daySecond) + '天'
            usedTimeSecond %= this.data.daySecond
        }
        if (usedTimeSecond > this.data.hourSecond) { //hour
            usedTimeStr += parseInt(usedTimeSecond / this.data.hourSecond) + '小时'
            usedTimeSecond %= this.data.hourSecond
        }
        if (usedTimeSecond > this.data.minuteSecond) { //minute
            usedTimeStr += parseInt(usedTimeSecond / this.data.minuteSecond) + '分'
            usedTimeSecond %= this.data.minuteSecond
        }
        return usedTimeStr
    },
    //banner页面跳转
    jumpNoticePage: function (event) {
        wx.navigateTo({
            url: '/pages/notice/index?id=' + event.currentTarget.dataset.id,
        })
    },
    jumpToExam() {
        if (!this.data.user) {
            app.doLogin()
        }
        wx.navigateTo({
            url: '/pages/choose-kemu/index'
        })
    },
    jumpToLogin() {
      wx.navigateTo({
        url: '/pages/login/index'
      })
    },
})